////////////////////////////////////////////////////////////////////////////////////
//
// file : ProcRock.cs
//
// proj : Tring
// auth : instil
// date : 111024
// desc : Setting rock properties
//
////////////////////////////////////////////////////////////////////////////////////

using UnityEngine;
using System.Collections;

public class ProcRock : MonoBehaviour {
    
    // Constants
    private const float ROCK_ROTATION_SPEED = 30.0f;
    private const float ROCK_ROTATION_SPEED_HALF = ROCK_ROTATION_SPEED * 0.5f;

    public Material[] materials;

	// Use this for initialization
	void Start () {
	    int nIndex = (int)(Random.value * materials.Length);
    
	    // Set material
        renderer.material = materials[nIndex];

        //Vector2 vOffset = renderer.sharedMaterial.mainTextureOffset;
        //vOffset.x += 0.25f * (float)(nIndex%4);
        //renderer.sharedMaterial.mainTextureOffset = vOffset;
    	
	    // Set rotation
	    rigidbody.AddTorque( getRockRotationSpeed() );
	    transform.Rotate( getRockRotationSpeed() );
	}
	
	// Update is called once per frame
	void Update () {
        // Destroy passed rocks
        if (transform.position.y < -5.0f)
        {
            //Destroy(this.gameObject);
            ProcRockPool.Instance.DeadRock(this.gameObject);
        }
	}
    
    private Vector3 getRockRotationSpeed()
    {
	    return new Vector3( Random.value * ROCK_ROTATION_SPEED - ROCK_ROTATION_SPEED_HALF,
					    Random.value * ROCK_ROTATION_SPEED - ROCK_ROTATION_SPEED_HALF,
					    Random.value * ROCK_ROTATION_SPEED - ROCK_ROTATION_SPEED_HALF);
    }
}
